package models

import "time"

type Comments struct {
	CommentID uint64    `gorm:"column:comment_id;primary_key;auto_increment" json:"comment_id"`
	Content   string    `gorm:"column:content;not null" json:"content" `
	UserID    uint64    `gorm:"column:user_id;not null" json:"user_id" `
	RecipeID  uint64    `gorm:"column:recipe_id;not null" json:"recipe_id"`
	CreatedAt time.Time `gorm:"column:create_at;autoCreateTime" json:"create_at"`
	UpdatedAt time.Time `gorm:"column:updated;autoUpdateTime" json:"updated_at"`
}

func (comments *Comments) Delete() error {
	return DB.Self.Delete(&comments).Error
}
func (comments *Comments) Create() error {
	return DB.Self.Create(&comments).Error
}

func (u *Comments) Update() error {
	return DB.Self.Model(&u).Update(&u).Error
}

// 查询，提供Id查询
func GetCommentsById(id uint64) (*Comments, error) {
	recipe := &Comments{}
	r := DB.Self.First(recipe, id)
	return recipe, r.Error
}

// 查询个人评论
func GetCommentsByUserId(userId uint64) ([]*Comments, error) {
	comments := make([]*Comments, 0)
	err := DB.Self.Where("user_id = ?", userId).Find(&comments).Error
	return comments, err
}

// 查询食谱下的评论
func GetCommentsByRecipeId(recipeId uint64) ([]*Comments, error) {
	comments := make([]*Comments, 0)
	err := DB.Self.Where("recipe_id = ?", recipeId).Find(&comments).Error
	return comments, err
}
